<?php

namespace app\xf\controller;

use app\xf\model\FileModel;
use app\xf\model\MenuModel;
use app\xf\model\RuleModel;
use think\Controller;
use think\Request;

class MenuController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
//        $menus = $this->getAllMenu();
        $menus = MenuModel::select();
        $rule = RuleModel::select();
        $data['menu'] = $menus;
        $data['rules'] = $rule;
        return $this->SuccessReturn('获取成功', $data);
    }

    public function getAllMenu($fid = 0)
    {
        $menus = MenuModel::where('fid', $fid)
            ->with('rules')
            ->select();

        if (count($menus) > 0) {
            foreach ($menus as $v) {

                //获取下级
                $res = MenuModel::where('fid', $v['id'])
                    ->find();
                $v['menu'] = true;

                if ($res) {
                    $v['children'] = $this->getAllMenu($v['id']);
                }
            }
            return $menus;
        } else {
            return $menus;
        }
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post();
        //获取参数
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $rules = array_key_exists('rules', $data) ? $data['rules'] : [];
        $fid = array_key_exists('fid', $data) ? $data['fid'] : 0;
        //判断是否存在相同的菜单名称
        $oldmenu = MenuModel::where('name', $name)->where('fid', $fid)->find();
        if ($oldmenu) return $this->ErrorReturn(400, '存在相同名称 请重新填写');
        //添加菜单
        $menu = new MenuModel();
        $menu->name = $name;
        $menu->rules = $rules;
        $menu->fid = $fid;
        $menu->save();
        return $this->SuccessReturn('添加成功', $menu);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->put();

        //获取参数
        $id = array_key_exists('id', $data) ? $data['id'] : 0;
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $rules = array_key_exists('rules', $data) ? $data['rules'] : "";
        $fid = array_key_exists('fid', $data) ? $data['fid'] : "";
        if ($id == 0) return $this->ErrorReturn(403, '缺少id参数');
        //查询菜单
        $menu = MenuModel::find($id);
        if (!$menu) return $this->ErrorReturn(500, '未找到当前菜单');

        //查询是否相同
        $issetdata_sql = MenuModel::where('name', $name);
        if ($fid != '') {
            $issetdata_sql->where('fid', $fid);
        } else {
            $issetdata_sql->where('fid', $menu->fid);
        };
        $issetdata = $issetdata_sql->find();
        if ($issetdata && $issetdata->id != $id) return $this->ErrorReturn(400, '存在相同的菜单名称 请重新填写');

        if ($name != '') $menu->name = $name;
        if ($rules != '') $menu->rules = $rules;
        if ($fid != '') $menu->fid = $fid;
        $menu->save();
        return $this->SuccessReturn('修改成功', $menu);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->post('id');
        if ($id == 0) return $this->ErrorReturn(400, '菜单id无效');
        //查询是否存在此用户
        $menu = MenuModel::where('id', $id)->find();
        if (!$menu) return $this->ErrorReturn(404, '找不到此菜单');
        $menu->delete();
        return $this->SuccessReturn('删除成功');
    }
}
